const puppeteer = require("puppeteer");
const url  = "https://movie.douban.com/top250"
async function run(){
  // 1.创建一个浏览器的实例
  const browser = await puppeteer.launch();
  // 2.在浏览器中新建一个页面
  const page = await browser.newPage();
  // 3.使用page.goto()函数打开一个页面
  await page.goto(url);
  // 4.让页面等待2s
  await page.waitFor(2000);
  // 5.进入页面,获取DOM
  let res = await page.evaluate(()=>{
    let $ =window.jQuery;
    let items = $(".grid_view .item");
    let links = [];
    if(items.length>0){
    items.each((index,value)=>{
       let pic  = $(value).find(".pic img").attr("src");
       let title = $(value).find(".info .hd .title").html();
       links.push({
         pic,
         title
       })
    })
    }
    return links;
  })
  console.log(res);
  browser.close()
  return res;
}
module.exports = run;